﻿@function gray_bg($color, $endcolor) {
  $red: "";
  $blue: "";
  $green: "";
  $end_red: red($endcolor);
  $end_blue: blue($endcolor);
  $end_green: green($endcolor);
  $max: "";
  $mid: "";
  $min: "";

  @if $end_red > $end_green {
    @if $end_red > $end_blue {
      $max: $end_red;

      @if $end_green > $end_blue {
        $min: $end_blue;
        $mid: $end_green;
      }

      @else {
        $min: $end_green;
        $mid: $end_blue;
      }
    }

    @else {
      $mid: $end_red;
      $min: $end_green;
      $max: $end_blue;
    }
  }

  @else {
    @if $end_green > $end_blue {
      $max: $end_green;

      @if $end_red > $end_blue {
        $min: $end_blue;
        $mid: $end_red;
      }

      @else {
        $min: $end_red;
        $mid: $end_blue;
      }
    }

    @else {
      $min: $end_red;
      $mid: $end_green;
      $max: $end_blue;
    }
  }

  @if red($color) > green($color) {
    @if red($color) > blue($color) {
      $red: $max;

      @if green($color) > blue($color) {
        $blue: $min;
        $green: $mid;
      }

      @else {
        $green: $min;
        $blue: $mid;
      }
    }

    @else {
      $red: $mid;
      $green: $min;
      $blue: $max;
    }
  }

  @else {
    @if green($color) > blue($color) {
      $green: $max;

      @if red($color) > blue($color) {
        $blue: $min;
        $red: $mid;
      }

      @else {
        $red: $min;
        $blue: $mid;
      }
    }

    @else {
      $red: $min;
      $green: $mid;
      $blue: $max;
    }
  }

  @return rgb($red, $green, $blue);
}
